//
//  MultiTouch2OSC
//  2009 Michael Gubik.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


#import "MTVisualizingView.h"
#import <OpenGL/OpenGL.h>

@implementation MTVisualizingView

@synthesize mainRenderer;

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
		appController = [NSApp delegate];
    }
    return self;
}

- (void)dealloc {
	[pool release];
	[super dealloc];
}

- (void)prepareRendererAndOpenGLContextThenStartRendering {
	pool = [[NSAutoreleasePool alloc] init]; // Top-level pool
	
	NSOpenGLPixelFormatAttribute attributes[] = {	NSOpenGLPFAAccelerated, 
													NSOpenGLPFADoubleBuffer,
													NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)24, 
													NSOpenGLPFANoRecovery,
													(NSOpenGLPixelFormatAttribute)0
	};
	
	GLint swapInterval = 1; // only swap during vertical retrace
	GLint opaque = 0; 
	
	mainPixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; 
	
	mainGLContext = [[NSOpenGLContext alloc] initWithFormat:mainPixelFormat shareContext:nil];
	[mainGLContext setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
	[mainGLContext setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
	[mainGLContext setView:self];
	CGLLockContext([mainGLContext CGLContextObj]);
	[mainGLContext makeCurrentContext];
	
	glClearColor(0.0, 0.0, 0.0, 0.0);
	
	composition = [[QCComposition compositionWithFile:[[NSBundle mainBundle] pathForResource:@"TouchVisualizer" ofType:@"qtz"]] retain];
	
	mainRenderer = [[QCRenderer alloc] initWithCGLContext:[mainGLContext CGLContextObj] pixelFormat:[mainPixelFormat CGLPixelFormatObj] colorSpace:CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB) composition:composition];
	
	CGLUnlockContext([mainGLContext CGLContextObj]);
	[self render];
}


- (void)render {
	NSTimeInterval startTime=0;
	while(1){
		NSTimeInterval  time = [NSDate timeIntervalSinceReferenceDate];
		
		if(startTime == 0)
		{
			startTime = time;
			time = 0;
		}
		else
			time -= startTime;
		
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		
		CGLLockContext([mainGLContext CGLContextObj]);
		[[appController lockForCompositionInput] lock];
		[mainRenderer renderAtTime:time arguments:nil];
		[[appController lockForCompositionInput] unlock];
		[mainGLContext flushBuffer];
		CGLUnlockContext([mainGLContext CGLContextObj]);
		
		usleep(16777);  
	}
	
}

- (void)resize {
	NSRect frame = [[self superview] frame];
	CGDirectDisplayID   displayID = CGMainDisplayID();
	CVDisplayLinkRef displayLink;
	
	CVDisplayLinkCreateWithCGDisplay (displayID, &displayLink);
	BOOL running = CVDisplayLinkIsRunning(displayLink);

	CGLLockContext([mainGLContext CGLContextObj]);
	[[appController lockForCompositionInput] lock];
	[mainGLContext makeCurrentContext];
	[self setFrame:frame];

	
	if (running)
		CVDisplayLinkStop(displayLink);	
	[mainGLContext update];
	glViewport(0, 0, frame.size.width, frame.size.height);
	CVDisplayLinkStart(displayLink);
	
	[[appController lockForCompositionInput] unlock];
	CGLUnlockContext([mainGLContext CGLContextObj]); 

}


- (void)drawRect:(NSRect)dirtyRect {
}

@end
